// pages/seatSelect/seat.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    seatSize: 30,
    seatRow: 9,
    seatCol: 8,
    seatArray: [],
    midlineh: 0,
    show: [],
    name: '',
    sdate: '',
    edate: '',
    vid: '',
    mid: '',
    rid: '',
    language: '',
    price: 0,
    totalprice: 0,
    selected: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var page = this;
    let seatArray = Array(this.data.seatRow).fill(0).map(() => Array(this.data.seatCol).fill(0));
    wx.getStorage({
      key: 'userInfo',
      success(res) {
        page.setData({
          rid: res.data.id
        })        
      }
    })
    this.setData({
      seatArray: seatArray
    })
    this.setData({
      mid: options.mid,
      name: options.name,
      price: options.price,
      sdate: options.startDate,
      vid: options.vid,
      language: options.language,
      id: options.id,
    })
    wx.request({
      url: app.globalData.url + '/wechatapi/isbought?Mid=' + this.data.id,
      success(res) {
        for (var i = 0; i < res.data.length; i++) {
          var item = page.data.seatArray;
          item[res.data[i].rws - 1][res.data[i].clmn - 1] = 2; //0  可选座位   (白色) 1  已选座位   (绿色) 2  已购票座位 (红色)
          page.setData({
            seatArray: item
          })
        }
      }
    })
    wx.request({
      url: app.globalData.url + '/wechatapi/getvideohellbyid?id=' + this.data.vid,
      success(res) {
        page.setData({
          seatRow: res.data.rws,
          seatCol: res.data.clmns
        })
      }
    })

    wx.createSelectorQuery().selectAll('.inner-seat-wrapper').boundingClientRect(function (rect) {
      page.setData({
        midlineh: rect[0].height + 20
      })
    }).exec()
  },
  handleChooseSeat(event) {
    var row = parseInt(event.target.dataset.row);
    var col = parseInt(event.target.dataset.col);
    let seatValue = this.data.seatArray[row][col];
    let newArray = this.data.seatArray;
    //如果是已购座位，直接返回
    if (seatValue === 2) return
    //如果是已选座位点击后变未选
    if (seatValue === 1) {
      this.setData({
        totalprice: this.data.totalprice - parseFloat(this.data.price)
      })
      newArray[row][col] = 0
      for (let index = 0; index < this.data.show.length; index++) {
        if (this.data.show[index].row === row && this.data.show[index].col === col) {
          var show = this.data.show;
          show.splice(index, 1)
          this.setData({
            show
          })
        }
      }
    } else if (seatValue === 0) {
      this.setData({
        totalprice: this.data.totalprice + parseFloat(this.data.price)
      })
      newArray[row][col] = 1
      var item = {
        row: row,
        col: col
      }
      var show = [...this.data.show, {
        ...item
      }]
      this.setData({
        show
      })
    }
    this.setData({
      seatArray: newArray
    })
  },
  onClose(event) {
    for (let index = 0; index < this.data.show.length; index++) {
      if (this.data.show[index].row === event.target.dataset.item.row && this.data.show[index].col === event.target.dataset.item.col) {
        var show = this.data.show;
        let newArray = this.data.seatArray;
        show.splice(index, 1)
        newArray[event.target.dataset.item.row][event.target.dataset.item.col] = 0
        this.setData({
          show,
          seatArray: newArray
        })
      }
    }
  },
  buyticket(e) {
    var page = this;
    Date.prototype.format = function (format) {
      var date = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S+": this.getMilliseconds()
      };
      if (/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
      }
      for (var k in date) {
        if (new RegExp("(" + k + ")").test(format)) {
          format = format.replace(RegExp.$1, RegExp.$1.length == 1 ?
            date[k] : ("00" + date[k]).substr(("" + date[k]).length));
        }
      }
      return format;
    }
    for(var i=0;i<this.data.seatRow;i++){
      for(var j=0;j<this.data.seatCol;j++){
        if(this.data.seatArray[i][j]===1){
          wx.request({
            url: app.globalData.url + '/wechatapi/addorder?Fid='+this.data.mid+'&Mid='+this.data.id+'&Rid='+this.data.rid+'&Vid='+this.data.vid+'&clmn='+(j+1)+'&price='+this.data.price+'&rws='+(i+1)+'&time='+(new Date()).format('yyyy-MM-dd hh:mm:ss'),
          })
        }
      }
    }
    Toast.loading({
      mask: true,
      message: '加载中...',
      context: this,
      onClose: () => {
        wx.navigateBack({
          delta: 1
        });
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})